package com.qiguliuxing.dts.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qiguliuxing.dts.admin.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.service.base.DtsSearchHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.db.domain.DtsSearchHistory;

@RestController
@RequestMapping("/admin/history")
@Api(tags = ApiAdminConsts.History.PARENT)
public class AdminHistoryController {
	private static final Logger logger = LoggerFactory.getLogger(AdminHistoryController.class);

	@Autowired
	private DtsSearchHistoryService searchHistoryService;

	@RequiresPermissions("admin:history:list")
	@RequiresPermissionsDesc(menu = { "用户管理", "搜索历史" }, button = "查询")
	@ApiOperation(value = ApiAdminConsts.History.LIST)
	@GetMapping("/list")
	@WebLog(description = ApiAdminConsts.History.LIST,excludes = {LogParamsConsts.RESPONSE})
	public Object list(
			@ApiParam(value = "用户id") String userId,
			@ApiParam(value = "关键字") String keyword,
			@ApiParam(value = "页码",defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
			@ApiParam(value = "分页大小",defaultValue = "10") @RequestParam(defaultValue = "10") Integer limit,
			@ApiParam(value = "排序字段",defaultValue = "add_time") @Sort @RequestParam(defaultValue = "add_time") String sort,
			@ApiParam(value = "排序规则",defaultValue = "desc") @Order @RequestParam(defaultValue = "desc") String order) {
		List<DtsSearchHistory> footprintList = searchHistoryService.querySelective(userId, keyword, page, limit, sort,
				order);
		long total = PageInfo.of(footprintList).getTotal();
		Map<String, Object> data = new HashMap<>();
		data.put("total", total);
		data.put("items", footprintList);
		return ResponseUtils.ok(data);
	}
}
